package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.service.ReportService;
import com.sky.vo.TurnoverReportVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrderMapper orderMapper;
    /**
     * 营业额统计接口
     * @param begin
     * @param end
     * @return
     */
    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {
        List<String> dateList = new ArrayList<>();
        List<String> turnoverList = new ArrayList<>();

        //1.封装日期数据
        // 计算 start 到 end 的总天数
        long totalDays = ChronoUnit.DAYS.between(begin, end);

        // 循环生成每一天（从 start 开始，累加 0 到 totalDays 天）
        for (long i = 0; i <= totalDays; i++) {
            LocalDate currentDate = begin.plusDays(i);
            // 转换为 "yyyy-MM-dd" 格式并加入列表（LocalDate 默认 toString() 就是该格式）
            dateList.add(currentDate.toString());
        }

        String dateStr = String.join(",", dateList);
        //封装日期数据
        TurnoverReportVO turnoverReportVO = new TurnoverReportVO();
        turnoverReportVO.setDateList(dateStr);

        //2.封装每日营业额数据
        for (long i = 0; i <= totalDays; i++) {
            LocalDate currentDate = begin.plusDays(i);
            LocalDateTime min = LocalDateTime.of(currentDate, LocalTime.MIN);
            LocalDateTime max = LocalDateTime.of(currentDate, LocalTime.MAX);
            Map map = new HashMap();
            map.put("min",min);
            map.put("max",max);
            map.put("status", Orders.COMPLETED);
            Double statistics = orderMapper.getStatistics(map);
            statistics = statistics == null ? 0.0 : statistics;
            String s = statistics.toString();
            turnoverList.add(s);
        }
        String turnoverStr = String.join(",", turnoverList);
        turnoverReportVO.setTurnoverList(turnoverStr);

        return turnoverReportVO;
    }
}
